package com.hema.auction.widget.dialog;

import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.hema.auction.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author by john
 */
public class LoadingDialog extends DialogFragment {

    private static final String TAG = "LoadingDialog";

    @BindView(R.id.tv_tip)
    TextView tvTip;

    public static LoadingDialog newInstance() {
        LoadingDialog fragment = new LoadingDialog();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View view = inflater.inflate(R.layout.layout_dialog_loading, container);
        ButterKnife.bind(this, view);
        return view;
    }

    public void show(android.support.v4.app.FragmentManager manager) {
//        LoadingDialog dialog = LoadingDialog.newInstance();
//        FragmentTransaction ft = manager.beginTransaction();
//        ft.add(dialog, "SignDialog");
//        ft.commitAllowingStateLoss();
        show(manager, TAG);
    }


    /**
     * 设置提示文字 【默认显示为：加载中...】 如果传递Null或者空字符串，则显示默认的文字
     *
     * @param tipMessage
     */
    public void setMessage(String tipMessage) {
        if (tipMessage != null && !tipMessage.equals("")) {
            if (tvTip != null) {
                tvTip.setText(tipMessage);
                tvTip.setVisibility(View.VISIBLE);
            }
        }
    }

    public void setTipMessage(int resId) {
        String tipMessage = getContext().getString(resId);
        if (tipMessage != null && !tipMessage.equals("")) {
            tvTip.setText(resId);
            tvTip.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
    }
}
